package com.hk.core.autoconfigure.data;

import com.hk.commons.multischema.DataSourceType;
import com.hk.commons.util.StringUtils;
import com.hk.core.jdbc.tenants.TenantIdResolver;
import com.hk.core.web.Webs;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * @author Kevin
 * @date 2024-04-21 12:48
 */
public final class RequestUriResolver implements TenantIdResolver {

    private Map<String, String> requestUriTenantIdMap;

    public RequestUriResolver setRequestUriTenantIdMap(Map<String, String> requestUriTenantIdMap) {
        this.requestUriTenantIdMap = Collections.unmodifiableMap(requestUriTenantIdMap);
        return this;
    }

    @Override
    public String resolve(String value) {
        if (StringUtils.equals(DataSourceType.REQUEST_URI, value)) {
            var request = Webs.getHttpServletRequest();
            if (Objects.nonNull(request)) {
                return requestUriTenantIdMap.get(request.getRequestURI());
            }
        }
        return null;
    }
}
